﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using IWebWFCore;
using System.Collections.Generic;

namespace SampleWorkflow
{
    public sealed partial class Workflow : SequentialWorkflowActivity
    {
        public string Status;
        public string TmpStatus;
        public Boolean isTimeToStop = false;
        public string UserName;
        public string Password;
        public bool boolAuthendicated;
        public Guid WorkflowInstanceID;

        public Workflow()
        {
            InitializeComponent();
        }

        private void SetWorkflowInstanceId(object sender, EventArgs e)
        {
            WorkflowInstanceID = this.WorkflowInstanceId;
        }

        // This method will return the value of isTimeToStop
        // The value of isTimeToStop will be set by other
        // methods in the workflow
        private void CheckIsTimeToStop(object sender, ConditionalEventArgs e)
        {
            e.Result = !(isTimeToStop);
        }

        private void CheckAuthentication(object sender, ConditionalEventArgs e)
        {
            e.Result = (IsAuthenticated(UserName, Password));
        }

        // When this method is called isTimeToStop will be set to true
        // This will cause the workflow to terminate
        private void SetisTimeToStop(object sender, EventArgs e)
        {
            //Stop the WhileActivity
            isTimeToStop = true;
        }

        private void SetStatus(object sender, EventArgs e)
        {
            if (IsAuthenticated(UserName, Password))
            {
                Status = TmpStatus;
            }
        }

        #region IsAuthenticated
        bool IsAuthenticated(string TmpUserName, string TmpPassword)
        {
            bool Authenticated = false;
            // Try to authendicate
            IWebWFDataContext IWebWFDataContext = new IWebWFDataContext();

            // Get allowed Login Failures
            int intAllowedLoginFailures = Convert.ToInt32(IWebWFDataContext.IWebWFSettings.FirstOrDefault(x => x.Setting == "MaxLoginAttempts").SettingValue);

            var result = from WFUser in IWebWFDataContext.IWebWFUsers
                         where WFUser.UserName == TmpUserName
                         select WFUser;

            IWebWFUser IWebWFUser = (IWebWFUser)result.FirstOrDefault();

            if (IWebWFUser != null)
            {
                if (IWebWFUser.Password == TmpPassword)
                {
                    if (!IWebWFUser.Active)
                    {
                        // Log the event
                        ApplicationLog.AddToLog(IWebWFUser.UserName, "Login attempt to SampleWorkflow. Account Not Active");
                    }
                    else if (IWebWFUser.Active)
                    {
                        // Set any possible failed login attempts back to 0
                        IWebWFUser.LoginFailures = 0;
                        IWebWFDataContext.SubmitChanges();

                        Authenticated = true;
                    }
                }
                else
                {
                    // Bad Password
                    IWebWFUser.LoginFailures++;

                    // Is this user locked out?
                    if (IWebWFUser.LoginFailures >= intAllowedLoginFailures)
                    {
                        IWebWFUser.Active = false;

                        // Log the event
                        ApplicationLog.AddToLog(IWebWFUser.UserName, "Login attempt to SampleWorkflow. Account has been locked out because of too many invalid login attempts");
                    }
                    else
                    {
                        // Log the event
                        ApplicationLog.AddToLog(IWebWFUser.UserName, String.Format("Login attempt to SampleWorkflow. The password entered is incorrect. Account has {0} more attempts", (intAllowedLoginFailures - IWebWFUser.LoginFailures).ToString()));
                    }

                    IWebWFDataContext.SubmitChanges();
                }
            }
            else
            {
                ApplicationLog.AddToLog(TmpUserName, "Login attempt to SampleWorkflow. Bad Username");
            }

            return Authenticated;
        }
        #endregion
    }
}
